package swingTests;
import java.awt.Component;
import java.util.Iterator;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTextField;

/**
 * 
 */

/**
 * @author ganeshT
 */
public class COmponentTest {
    /**
     * If all fields blank.
     * @param fldList
     *            the fld list
     * @return true, if successful
     */
    private boolean ifAllFieldsBlank(List<JComponent> fldList) {
        boolean isBlank = true;
        Iterator<JComponent> itr = fldList.iterator();
        while (itr.hasNext()) {
            JComponent curCmpnt = (JComponent) itr.next();
            // check for JTextField
            if (curCmpnt instanceof JTextField) {
                JTextField curFld = (JTextField) curCmpnt;
                if (!curFld.getText().trim().isEmpty()) {
                    // date field having mask "__/__/__" is also blank
                    if (!curFld.getText().trim().equals("__/__/__")) {
                        isBlank = false;
                        return isBlank;
                    }
                }
            }
            // check for JTableDataComboBox
            if (curCmpnt instanceof JComboBox) {
                JComboBox curFld = (JComboBox) curCmpnt;
                if (curFld.getSelectedIndex() != -1) {
                    if (!curFld.getSelectedItem().toString().trim().isEmpty()) {
                        isBlank = false;
                        return isBlank;
                    }
                }
            }
        }
        return isBlank;
    }

    /**
     * Clear all fields.
     * @param parent
     *            the parent
     */
    private void clearAllFields(Component parent) {
        if (parent instanceof JTextField) {
            if (((JTextField) parent).isEditable()) {
                ((JTextField) parent).setText("");
            }
        } else if (parent instanceof JComboBox) {
            ((JComboBox) parent).removeAllItems();
        } else if (parent instanceof JComponent) {
            Component[] children = ((JComponent) parent).getComponents();
            for (int i = 0; children.length > i; i++) {
                clearAllFields(children[i]);
            }
        }
    }

}
